package com.example.ordersystem;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.ordersystem.pojo.User;
import com.example.ordersystem.utils.ListDataSave;

import java.util.List;

/*
 * 登录页面
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {


    private Button login_btn_login;//登录
    private EditText login_editText_username;//用户名输入
    private EditText login_editText_password;//密码输入
    private TextView login_textView_register;//注册
    private TextView login_textView_forget_password;//忘记密码
    private TextView login_textView_message_checked;//短信验证
    private ImageView imageView_back;//返回user页面

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
    }

    //绑定
    public void initView() {

        imageView_back = findViewById(R.id.imageView_back);
        login_btn_login = findViewById(R.id.login_btn_login);
        login_editText_username = findViewById(R.id.login_editText_username);
        login_editText_password = findViewById(R.id.login_editText_password);
        login_textView_register = findViewById(R.id.login_textView_register);
        login_textView_forget_password = findViewById(R.id.login_textView_forget_password);
        login_textView_message_checked = findViewById(R.id.login_textView_message_checked);

        login_btn_login.setOnClickListener(this);
        login_textView_register.setOnClickListener(this);
        login_textView_forget_password.setOnClickListener(this);
        login_textView_message_checked.setOnClickListener(this);
        imageView_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_textView_register://注册
                startActivity(new Intent(this, RegisterActivity.class));
                finish();
                break;

            case R.id.imageView_back://返回
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                break;

            case R.id.login_btn_login://登录
                String username = login_editText_username.getText().toString().trim();
                String password = login_editText_password.getText().toString().trim();

                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {//判空
                    boolean flag = false;

                    List<User> list = ListDataSave.getUserList(this, username);

                    try {
                        for (User user : list) {
                            if (username.equals(user.getName()) && password.equals(user.getPassword())) {
                                flag = true;
                                break;
                            } else flag = false;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {

                        if (flag) {

                            ListDataSave.putUserLogin(this, username);
                            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                            intent = new Intent(this, MainActivity.class);
                            intent.putExtra("username", username);//传递参数(为了查询到该用户对象)
                            startActivity(intent);

                            finish();//销毁此Activity
                        } else Toast.makeText(this, "用户名或密码有误", Toast.LENGTH_SHORT).show();
                    }
                } else Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                break;

            case R.id.login_textView_forget_password:
                Toast.makeText(this, "忘记密码", Toast.LENGTH_SHORT).show();
                break;

            case R.id.login_textView_message_checked:
                Toast.makeText(this, "短信验证", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
